<script lang="ts" setup name="XtxCarousel">
import { PropType, ref, onMounted, onBeforeUnmount } from 'vue'
import { BannerItem } from '@/types/data'
const props = defineProps({
  list: {
    type: Array as PropType<BannerItem[]>,
    required: true
  },
  autoPlay: {
    type: Boolean,
    default: true
  },
  currTime: {
    type: Number,
    default: 2000
  }
})
// defineProps<{
//   list:BannerItem[]
// }>()
// 默认显示第一张图
const currIndex = ref(0)
// 左右点击与小圆点点击功能
const changeCurrIndex = (step: number) => {
  const result = currIndex.value + step
  // 取值范围：0   props.list.length-1
  // 小于0  currIndex.value=props.list.length-1
  // 大于props.list.length-1    currIndex.value=0
  if (result < 0) {
    currIndex.value = props.list.length - 1
  } else if (result > props.list.length - 1) {
    currIndex.value = 0
  } else {
    currIndex.value = result
  }
}
// 自动轮播
// 开启自动轮播
let interval = 0
const play = () => {
  if (!props.autoPlay) {
    return
  }
  clearInterval(interval)
  // ts限制
  interval = window.setInterval(() => {
    changeCurrIndex(1)
  }, props.currTime)
}
// 关闭自动轮播
const stop = () => {
  clearInterval(interval)
}
// 等dom渲染完成后开启
onMounted(() => {
  play()
})
// 等组件销毁前移除setInterval
onBeforeUnmount(() => {
  stop()
})
</script>

<template>
  <div class="xtx-carousel" @mouseenter="stop" @mouseleave="play">
    <ul class="carousel-body">
      <li
        class="carousel-item"
        :class="{ fade: index === currIndex }"
        v-for="(item, index) in list"
        :key="item.id"
      >
        <RouterLink to="/">
          <img :src="item.imgUrl" alt="" />
        </RouterLink>
      </li>
    </ul>
    <a
      href="javascript:;"
      @click="changeCurrIndex(-1)"
      class="carousel-btn prev"
      ><i class="iconfont icon-angle-left"></i
    ></a>
    <a href="javascript:;" class="carousel-btn next" @click="changeCurrIndex(1)"
      ><i class="iconfont icon-angle-right"></i
    ></a>
    <div class="carousel-indicator">
      <!-- 5   [1,2,3,4,5] -->
      <span
        :class="{ active: currIndex === item - 1 }"
        v-for="item in list.length"
        :key="item"
        @mouseenter="currIndex = item - 1"
      ></span>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-carousel {
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;
  .carousel {
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background: #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev {
        left: 20px;
      }
      &.next {
        right: 20px;
      }
    }
  }
  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
</style>
